//
//  StatisticsViewController.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/10/14.
//

import UIKit
import ETKit

let kStatisticsCountCell = "StatisticsCountCell"
let kStatisticsPieChartCell = "StatisticsPieChartCell"


class StatisticsViewController: BaseTableViewController {
    
    var datas = [BaseSectionModel]()

    @IBOutlet weak var calcNumberLabel: UILabel!
    @IBOutlet weak var totalTimeLabel: UILabel!
    
    private let viewModel = StatisticsViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "统计"
        tableView.rowHeight = 80
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: kStatisticsCountCell, bundle: nil), forCellReuseIdentifier: kStatisticsCountCell)
        tableView.register(UINib(nibName: kYTDSegmentedCell, bundle: nil), forCellReuseIdentifier: kYTDSegmentedCell)
        tableView.register(StatisticsPieChartCell.self, forCellReuseIdentifier: kStatisticsPieChartCell)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        loadDatas()
        NotificationCenter.default.post(name: .refreshPieChart, object: nil)
    }
    
    private func loadDatas() {
        datas.removeAll()
        
        datas = viewModel.loadData()
        tableView.reloadData()
    }
}

extension StatisticsViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return datas.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas[section].items.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return datas[indexPath.section].items[indexPath.row].rowHeight
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 10
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = datas[indexPath.section].items[indexPath.row]
        if model is StatisticsCountCellModel {
            let cell = tableView.dequeueReusableCell(withIdentifier: kStatisticsCountCell, for: indexPath) as! StatisticsCountCell
            cell.model = model as? StatisticsCountCellModel
            return cell
        } else if model is YTDSegmentedCellModel {
            let cell = tableView.dequeueReusableCell(withIdentifier: kYTDSegmentedCell, for: indexPath) as! YTDSegmentedCell
            return cell
        } else if model is StatisticsPieChartCellModel {
            let cell = tableView.dequeueReusableCell(withIdentifier: kStatisticsPieChartCell, for: indexPath) as! StatisticsPieChartCell
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: kStatisticsCountCell, for: indexPath) as! StatisticsCountCell
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
    }
}
